Inboxに素早く投入するPage Menu A
Inboxに素早く投入するPage Menu A
おためし実装
ちゃんと動いたっぽい
code:script.ts
import { addToInbox } from "./mod.ts";
import type {
Scrapbox
declare const scrapbox: Scrapbox;
const project = "takker-memex";
const title = "メモ帳";
scrapbox.PageMenu.addMenu({
title: "Add to inbox",
image: "/assets/img/logo.svg",
onClick: async () => {
const item = window.prompt("Type item you wanna add to the inbox");
if (!item || item.trim() === "") return;
await addToInbox(project, title, item.split(/\s+/));
},
});
code:mod.ts
import {
patch,
useStatusBar,
openInTheSameTab,
} from "../scrapbox-userscript-std/mod.ts";
import { delay as sleep } from "../deno_std%2Fasync/mod.ts";
export async function addToInbox(
project: string, title: string, items: string[]
): Promise<void> {
const { render, dispose } = useStatusBar();
try {
render(
{ type: "spinner" },
{ type: "text", text: Adding ${items.length} items... },
);
await patch(project, title, (lines) => [
...lines.map((line) => line.text),
...items,
]);
render(
{ type: "check-circle" },
{ type: "text", text: "Added to the inbox." },
);
} catch(e: unknown) {
render(
{ type: "exclamation-triangle" },
{ type: "text", text: "Failed to add (see console). Write directory instead." },
);
console.error(e);
openInTheSameTab(project, title, items.join("\n"));
} finally {
await sleep(1000);
dispose();
}
}